home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapte10 / keepstr.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  7KB  |  239 lines

  1.  
  2. #include <windows.h>  
  3. #include "keepstr.h"  
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17. HWND      hWindow = NULL;
  18.  
  19. LPCTSTR lpszAppName = "MyApp";
  20. LPCTSTR lpszTitle   = "DdeKeepStringHandle()"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106. HDDEDATA CALLBACK DdemlCallback( UINT     uType,
  107.                                  UINT     uFmt,    
  108.                                  HCONV    hconv,    
  109.                                  HSZ      hsz1,    
  110.                                  HSZ      hsz2,    
  111.                                  HDDEDATA hdata,    
  112.                                  DWORD    dwData1,
  113.                                  DWORD    dwData2 );
  114.  
  115.  
  116. DWORD ddeInst    = 0;
  117. HSZ   hszBase    = NULL;
  118. HSZ   hszService = NULL;
  119.  
  120. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  121. {
  122. static char        szBuf[128];
  123. static char        szBase[40];
  124. static char        szService[40];
  125. static PAINTSTRUCT ps;
  126.  
  127.    switch( uMsg )
  128.    {
  129.       case WM_CREATE :
  130.               // Initialize DDEML.
  131.               //..................
  132.               if ( DdeInitialize( &ddeInst, DdemlCallback, 
  133.                    APPCMD_CLIENTONLY, 0 ) != DMLERR_NO_ERROR )
  134.               {
  135.                  MessageBox( hWnd, "Could not initialize DDEML.", 
  136.                              NULL, MB_OK | MB_ICONSTOP );
  137.                  return( -1 );
  138.               }
  139.               hWindow = hWnd;
  140.               break;
  141.  
  142.       case WM_COMMAND :
  143.               switch( LOWORD( wParam ) )
  144.               {
  145.                  case IDM_ABOUT :
  146.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  147.                         break;
  148.  
  149.                  case IDM_EXIT :
  150.                         DestroyWindow( hWnd );
  151.                         break;
  152.               }
  153.               break;
  154.  
  155.       case WM_USER :
  156.               InvalidateRect( hWnd, NULL, TRUE );
  157.               break;
  158.  
  159.       case WM_PAINT :
  160.               BeginPaint( hWnd, &ps );
  161.               if ( hszBase && hszService )
  162.               {
  163.                  DdeQueryString( ddeInst, hszBase, szBase, 
  164.                                  sizeof( szBase ), CP_WINANSI );
  165.  
  166.                  DdeQueryString( ddeInst, hszService, szService,
  167.                                  sizeof( szService ), CP_WINANSI );
  168.  
  169.                  wsprintf( szBuf, "DDE Base = %s, Service = %s", 
  170.                                   (LPTSTR)szBase, (LPTSTR)szService );
  171.  
  172.                  TextOut( ps.hdc, 5, 5, szBuf, strlen( szBuf ) );
  173.  
  174.                  DdeFreeStringHandle( ddeInst, hszBase );
  175.                  DdeFreeStringHandle( ddeInst, hszService );
  176.               }
  177.               EndPaint( hWnd, &ps );
  178.               break;
  179.       
  180.       case WM_DESTROY :
  181.               DdeUninitialize( ddeInst );
  182.               PostQuitMessage(0);
  183.               break;
  184.  
  185.       default :
  186.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  187.    }
  188.  
  189.    return( 0L );
  190. }
  191.  
  192.  
  193. HDDEDATA CALLBACK DdemlCallback( UINT     uType,
  194.                                  UINT     uFmt,    
  195.                                  HCONV    hconv,    
  196.                                  HSZ      hsz1,    
  197.                                  HSZ      hsz2,    
  198.                                  HDDEDATA hdata,    
  199.                                  DWORD    dwData1,
  200.                                  DWORD    dwData2 )
  201. {
  202.    switch( uType )
  203.    {
  204.       case XTYP_REGISTER : 
  205.              DdeKeepStringHandle( ddeInst, hsz1 );
  206.              DdeKeepStringHandle( ddeInst, hsz2 );
  207.              hszBase    = hsz1;
  208.              hszService = hsz2;
  209.              SendMessage( hWindow, WM_USER, 0, 0 );
  210.              break;
  211.    }
  212.  
  213.    return( NULL );
  214. }
  215.  
  216.  
  217. LRESULT CALLBACK About( HWND hDlg,           
  218.                         UINT message,        
  219.                         WPARAM wParam,       
  220.                         LPARAM lParam)
  221. {
  222.    switch (message) 
  223.    {
  224.        case WM_INITDIALOG: 
  225.                return (TRUE);
  226.  
  227.        case WM_COMMAND:                              
  228.                if (   LOWORD(wParam) == IDOK         
  229.                    || LOWORD(wParam) == IDCANCEL)    
  230.                {
  231.                        EndDialog(hDlg, TRUE);        
  232.                        return (TRUE);
  233.                }
  234.                break;
  235.    }
  236.  
  237.    return (FALSE); 
  238. }
  239.